<?php defined('PATH') or exit();

class Model_Store extends SimpleO {

	const TYPE_ITEM = 0;
	const TYPE_PREMMY = 1;

	private $data = array('id' => null, 'name' => null, 'price' => null, 'unit' => null, 'amount' => null, 'type' => null, 'time' => null);

	public function __construct() {
		parent::__construct();
	}

	public function __get($_key) {
		if (is_array($this->data) && array_key_exists($_key, $this->data))
			return $this->data[$_key];
	}

	public function __set($_key, $_value) {
		if (is_array($this->data) && array_key_exists($_key, $this->data))
			$this->data[$_key] = $_value;
	}
	
	public function load($_id) {
		$this->data = $this->db->query('SELECT * FROM `__archez_store_items` WHERE `__archez_store_items`.`id` = '.$this->db->quote($_id))->fetch();
		if ($this->data) {
			return true;
		}
		return false;
	}
	
	public function save() {
		if ($this->id)
			return (bool) $this->db->query('UPDATE `__archez_store_items` SET `name` = '.$this->db->quote($this->name).' AND `description` = '.$this->db->quote($this->description).' AND `price` = '.$this->db->quote($this->price).' AND `unit` = '.$this->db->quote($this->unit).' AND `itemid` = '.$this->db->quote($this->itemid).' AND `amount` = '.$this->db->quote($this->amount).' AND `type` = '.$this->db->quote($this->type).' AND `time` = '.$this->db->quote($this->time).' WHERE `id` = '.$this->db->quote($this->id));
		$this->db->query('INSERT INTO `__archez_store_items` (`name`, `description`, `price`, `unit`, `itemid`, `amount`, `type`) VALUES ('.$this->db->quote($this->name).', '.$this->db->quote($this->description).', '.$this->db->quote($this->price).', '.$this->db->quote($this->unit).', '.$this->db->quote($this->itemid).', '.$this->db->quote($this->amount).', '.$this->db->quote($this->type).', '.time().')');
		$id = $this->db->lastInsertId();
		if ($id) {
			$this->id = $id;
			return true;
		}
		return false;
	}

	public function delete() {
		if ($this->id) {
			 $this->db('DELETE FROM `__archez_store_items` WHERE `id` = '.$this->db->quote($_id));
			 return true;
		}
		return false;
	}
	
	public function isLoaded() {
		if ($this->id) {
			return true;
		}
		return false;
	}

}
?>